# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=pylint
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.5.2
pkgrel=1
pkgdesc="Analyzes Python code looking for bugs and signs of poor quality (mingw-w64)"
arch=('any')
url='https://pylint.org/'
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-python-astroid"
         "${MINGW_PACKAGE_PREFIX}-python-colorama"
         "${MINGW_PACKAGE_PREFIX}-python-mccabe"
         "${MINGW_PACKAGE_PREFIX}-python-isort")
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-astroid"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-six")
checkdepends=("${MINGW_PACKAGE_PREFIX}-mpdecimal"
              "${MINGW_PACKAGE_PREFIX}-python-pytest-runner")
optdepends=("${MINGW_PACKAGE_PREFIX}-tk: Pylint GUI"
            "${MINGW_PACKAGE_PREFIX}-graphviz: To have other output formats than dot or vcg")
options=('staticlibs' 'strip' '!debug')
install=${_realname}3-${CARCH}.install
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/PyCQA/pylint/archive/pylint-${pkgver}.tar.gz")
sha512sums=('c8281b1f3c23f75e2129e9b44630a43cc5c36ff562dcd474eda9e9a5db9a56538d73e52b3002fa16def681dd4e0d81103266bc01b28f2ca731d7aae069687901')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
 
  sed -i 's/pytest<3.3/pytest/' pylint-pylint-${pkgver}/setup.py
  pushd "${_realname}-${_realname}-${pkgver}"
    sed -e "s|/usr/bin/python$|${MINGW_PREFIX}/bin/python|" -i pylint/epylint.py pylint/__main__.py tests/input/noext tests/data/ascript
  popd
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${_realname}-${pkgver}" "python-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py pytest || warning "tests failed"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
